<template>
  <el-radio-group
    v-model="formData[_info.field]"
    v-loading="loading"
    :element-loading-spinner="loadingSpinner"
    :disabled="_info.disabled || false"
    element-loading-svg-view-box="0,0, 1024, 1024"
    @change="handleChange"
  >
    <el-radio
      v-for="item in optionList"
      :key="item.id"
      :label="item.id"
      :disabled="item.disabled"
    >
      {{ item.name }}
    </el-radio>
  </el-radio-group>
  <div v-if="_info.placeholder" class="radio-placeholder">
    {{ _info.placeholder }}
  </div>
</template>

<script setup>
import { getInfo, getRules, changeSelect, getOptions } from '../const';
import { loadingSpinner } from '@/assets/js/loadingIcon.js';

const props = defineProps({
  formInfo: {
    type: Object,
    default: () => {}
  },
  formData: {
    type: Object,
    default: () => {}
  },
  TrendsFormRef: {
    type: Object,
    default: null
  },
  topExpose: {
    type: Object,
    default: () => {}
  }
});

const _info = computed(() => getInfo(props.formInfo));
const _rules = computed(() => getRules(props.formInfo));

// 拿到下拉列表
const optionList = ref([]);
const loading = ref(false);
getOptions({ optionList, loading, props });

let oldValue = undefined;
watch(
  () => props.formData[_info.value.field],
  (newVal, oldVal) => (oldValue = oldVal),
  { deep: true, immediate: true }
);

// 选择事件
const domRef = ref();
function handleChange() {
  setTimeout(() => {
    changeSelect(props.formInfo, props.formData, {
      oldValue,
      domRef,
      topExpose: props.topExpose
    });
  }, 0);
}
</script>

<style lang="scss" scoped>
.radio-placeholder {
  color: #aaa;
  font-size: 12px;
  line-height: 16px;
}
</style>
